package com.chat.view;

import com.chat.Local;
import com.chat.service.ExitGroup;
import javafx.application.Platform;
import javafx.scene.control.Alert;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.text.Text;

import java.io.File;
import java.util.Iterator;
import java.util.Objects;

import static com.chat.view.MainWindowController.chat;
import static com.chat.view.GroupMemberController.member;
public class MemberListController {
    public ImageView touxiang;
    public Text nickname;
    public Text state;
    public String member_state;//成员地位
    public String username;
    public Label tishi;

    public void setTouxiang(String username) {
        File file1=new File("D:/chatproject/client/"+username+".png");
        this.touxiang.setImage(new Image("file:"+file1.getAbsolutePath()));
        this.username=username;
    }

    public void setNickname(String nickname) {
        this.nickname.setText(nickname);
    }

    public void setState(String state) {
           if(Objects.equals(state, "3"))
                this.state.setText("群主");
           else if(Objects.equals(state, "2"))
               this.state.setText("管理员");
           else this.state.setText("群成员");
           member_state=state;
    }
     public void KickOut(){
        if(Objects.equals(member_state, "3")){
            Platform.runLater(new Runnable() {
                @Override
                public void run() {
                    Alert alert = new Alert(Alert.AlertType.INFORMATION);
                    alert.setTitle("提示");
                    alert.setHeaderText("警告");
                    alert.setContentText("不能踢群主！");
                    alert.showAndWait();
                }
            });
        }
        else {
            int a = Integer.parseInt(member_state);
            int b = Integer.parseInt(Local.state);
            if(Objects.equals(username, Local.user.getUsername())){
                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {
                        Alert alert = new Alert(Alert.AlertType.INFORMATION);
                        alert.setTitle("提示");
                        alert.setHeaderText("警告");
                        alert.setContentText("不能踢自己！");
                        alert.showAndWait();
                    }
                });
            }
            else if (a >= b) {
                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {
                        Alert alert = new Alert(Alert.AlertType.INFORMATION);
                        alert.setTitle("提示");
                        alert.setHeaderText("警告");
                        alert.setContentText("您没有权限将他踢出！");
                        alert.showAndWait();
                    }
                });
            }
            else {
                ExitGroup eg=new ExitGroup();
                eg.set(Local.group.getId(),username);
                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {
                        Alert alert = new Alert(Alert.AlertType.INFORMATION);
                        alert.setTitle("提示");
                        alert.setHeaderText("恭喜");
                        alert.setContentText("成功踢出！");
                        alert.showAndWait();
                    }
                });
                chat.clear();
                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {
                        int line=0;
                        Iterator<MemberData> iter=member.iterator();
                        while(iter.hasNext()){
                            MemberData memberData=(MemberData) iter.next();
                            if(memberData.username.equals(username)){
                                member.remove(line);
                                break;
                            }
                            line++;
                        }
                    }
                });

            }
        }
     }
}
